﻿#include "addfile.h"
#include "ui_addfile.h"

AddFile::AddFile(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::AddFile)
{
    ui->setupUi(this);

    setWindowTitle(QStringLiteral("部署文件"));

    connect(ui->btn_select, SIGNAL(clicked(bool)), this, SLOT(btn_select()));
    connect(ui->btn_add, SIGNAL(clicked(bool)), this, SLOT(btn_add()));
}

void AddFile::btn_select() {

    QString pathName;

    pathName = QFileDialog::getOpenFileName(this, QStringLiteral("选择文件"),
                                            "D:/", tr("Txt files(*.txt);;All files(*.*)"));

    qDebug() << pathName;

    if(pathName.length()) ui->label_path->setText(pathName);



}

void AddFile::btn_add() {


    qDebug() << ui->label_path->text();

    if(!QString::compare(ui->label_path->text(), QStringLiteral("                        未选择……"))) {
        QMessageBox::critical(this, "Error", QStringLiteral("未选择有效文件！"), QStringLiteral("确定"));
        return;
    }




    QFile file(QDir::currentPath() + "/PMS_data/info.corgi");

    if(!file.open(QIODevice::Append | QIODevice::Text)) {
        QMessageBox::critical(this, "Error", QStringLiteral("文件打开失败！"), QStringLiteral("确定"));
        return;
    }

    QTextStream out(&file);


    QString txt = ui->lineEdit->text();

    out << ui->label_path->text() << "#&#";

    if(!txt.length()) out << "WIT-" << QString::number(QTime::currentTime().msec());
    else out << txt;


    out << '\n';


    file.close();


    QMessageBox::information(this, QStringLiteral("消息"), QStringLiteral("操作成功"), QStringLiteral("确定"));




}


AddFile::~AddFile()
{
    delete ui;
}
